import java.util.Arrays;
import java.util.Scanner;
import java.awt.*;
import java.io.*;

public class addrManage
{
	Scanner scan = new Scanner (System.in);
	Scanner scan2 = new Scanner (System.in);
	Scanner scan3 = new Scanner (System.in);

	addrCollection address = new addrCollection();

	String[] Name = new String[100];
	String[] alastName = new String[100];
	String[] afirstName = new String[100];
	String[] aposition = new String[100];
	String[] adepartment = new String[100];
	String[] acompanyname = new String[100];
	String[] aaddress = new String[100];
	String[] atelephonenumber = new String[100];
	String[] afaxnumber = new String[100];
	String[] amobilenumber = new String[100];
	String[] aemail = new String[100];
	String[] ahomepage = new String[100];

	int n=0;

	public void addrAdd()
	{	
		System.out.print ("last name: ");
		String lastName = scan.nextLine();

		System.out.print ("first name: "); 
		String firstName = scan.nextLine();

		System.out.print ("position: "); 
		String position = scan.nextLine();

		System.out.print ("deoartment: "); 
		String department = scan.nextLine();

		System.out.print ("company name: "); 
		String companyName = scan.nextLine();

		System.out.print ("address: "); 
		String addr = scan.nextLine();

		System.out.print ("telephone number: "); 
		String telephoneNum = scan.nextLine();

		System.out.print ("fax number: "); 
		String faxNum = scan.nextLine();

		System.out.print ("mobile number: "); 
		String mobileNum = scan.nextLine();

		System.out.print ("email address: "); 
		String email = scan.nextLine();

		System.out.print ("homepage name: "); 
		String homepage = scan.nextLine();

		address.addAddr(lastName, firstName, position, department, companyName, addr, telephoneNum, faxNum, mobileNum, email, homepage);

		Name[n] = lastName.concat(firstName);
		alastName[n] = lastName;
		afirstName[n] = firstName;
		aposition[n] = position;
		adepartment[n] = department;
		acompanyname[n] = companyName;
		aaddress[n] = addr;
		atelephonenumber[n] = telephoneNum;
		afaxnumber[n] = faxNum;
		amobilenumber[n] = mobileNum;
		aemail[n] = email;
		ahomepage[n] = homepage;

		n++;

		System.out.println(lastName + " " + firstName + "님의 정보가 추가되었습니다.");
	}

	public void pwChange(int pw)
	{
		System.out.print("Modify to password: ");
		int modifyPw = scan.nextInt();

		pwSave(modifyPw);
	}

	public void addrDisplay()
	{
		System.out.println("Lastname|Firstname|Position|Department|Address|Telephonenumber|Faxnumber|Mobilenumber|E=mail|Homepage");
		System.out.print("\n");

		String[] sortName = new String[address.count];

		for (int i = 0; i < address.count; i++) 
		{
			sortName[i] = Name[i];
		}

		Arrays.sort(sortName);

		for(int j=0; j < address.count; j++)
			for(int k=0; k < address.count; k++)
				if(sortName[j].equals(Name[k]))
					System.out.println(address.collection[k]);
	}

	public void addrView()
	{
		System.out.println("--------------------------------------------------------------");

		for(int q=0; q < address.collection.length; q++)
		{
			if(address.collection[q] !=null)
				System.out.println("["+(q+1)+"]"+alastName[q]+afirstName[q]);
		}

		System.out.println("--------------------------------------------------------------");
		System.out.println("번호 입력: ");

		int x = scan.nextInt();

		if(address.collection[x-1] !=null)
			System.out.println("["+(x)+"]"+address.collection[x-1]);
	}

	public void addrUpdate()
	{
		addrSearch(); //검색

		System.out.print("수정하고 싶은 주소록 일련번호 입력: ");

		int modifycount = scan.nextInt(); //주소록 일련번호 입력받음

		System.out.println("1. Last name 수정");
		System.out.println("2. First name 수정");
		System.out.println("3. Position 수정");
		System.out.println("4. Company name 수정");
		System.out.println("5. Department 수정");
		System.out.println("6. Address 수정");
		System.out.println("7. Telephone number 수정");
		System.out.println("8. Fax number 수정");
		System.out.println("9. Mobile number 수정");
		System.out.println("10. E-mail 수정");
		System.out.println("11. Homepage 수정");

		System.out.print("수정하고 싶은 정보의 일련번호 입력: ");
		int modifynum = scan.nextInt(); //정보의 일련번호 입력받음

		switch(modifynum)
		{
		case 1:
			System.out.print("수정할 Last name 입력: ");
			String mlastname = scan3.nextLine();
			alastName[modifycount-1] = mlastname;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 2:
			System.out.print("수정할 First name 입력: ");
			String mfirstname = scan3.nextLine();
			afirstName[modifycount-1] = mfirstname;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 3:
			System.out.print("수정할 Position 입력: ");
			String mposition = scan3.nextLine();
			aposition[modifycount-1] = mposition;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 4:
			System.out.print("수정할 Company name 입력: ");
			String mcompanyname = scan3.nextLine();
			acompanyname[modifycount-1] = mcompanyname;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 5:
			System.out.print("수정할 Department 입력: ");
			String mdepartment = scan3.nextLine();
			adepartment[modifycount-1] = mdepartment;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 6:
			System.out.print("수정할 Address 입력: ");
			String maddress = scan3.nextLine();
			aaddress[modifycount-1] = maddress;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 7:
			System.out.print("수정할 Telephone number 입력: ");
			String mtelephonenum = scan3.nextLine();
			atelephonenumber[modifycount-1] = mtelephonenum;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 8:
			System.out.print("수정할 Fax number 입력: ");
			String mfaxnum = scan3.nextLine();
			afaxnumber[modifycount-1] = mfaxnum;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 9:
			System.out.print("수정할 Mobile number 입력: ");
			String mmobilenum = scan3.nextLine();
			amobilenumber[modifycount-1] = mmobilenum;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 10:
			System.out.print("수정할 E-mail 입력: ");
			String memail = scan3.nextLine();
			aemail[modifycount-1] = memail;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;

		case 11:
			System.out.print("수정할 Homepage 입력: ");
			String mhomepage = scan3.nextLine();
			ahomepage[modifycount-1] = mhomepage;
			address.modiadd(modifycount-1, alastName[modifycount-1], afirstName[modifycount-1], aposition[modifycount-1], adepartment[modifycount-1], acompanyname[modifycount-1], aaddress[modifycount-1], atelephonenumber[modifycount-1], afaxnumber[modifycount-1], amobilenumber[modifycount-1], aemail[modifycount-1], ahomepage[modifycount-1]);
			break;
		default:
			System.out.print("번호를 잘못 입력하셨습니다.");
		}

		System.out.println("수정 되었습니다.");
	}

	public void addrSearch()
	{
		int[] searchresult = new int[100];
		int i=0;

		System.out.print("검색할 이름을 입력하세요: ");
		String inputName = scan2.nextLine();
		int searchCount = 0;

		for(int q=0; q < address.collection.length; q++)
		{
			if(address.collection[q] !=null)
			{
				if(Name[q].matches(".*"+inputName+".*"))
				{ 
					System.out.println("["+(q+1)+"]"+Name[q]);
					searchCount++;
					searchresult[i]=q;
					i++;
				}
			}
		}

		if(searchCount == 0)
			System.out.println("검색 결과가 없습니다.");
		else{
			System.out.println("--------------------------------------------------------------");
			System.out.print("번호 입력: ");

			int inputNum = scan.nextInt();
			int a=0;
			for(int w=0; w<i;w++)
			{
				if((inputNum-1)==searchresult[w]){
					if(address.collection[inputNum-1] !=null)
						System.out.println("["+(inputNum)+"]"+address.collection[inputNum-1]);
					a++;
				}
			}
			if(a==0)
				System.out.println("번호를 잘못 입력하였습니다.");
		}
	}

	public void addrDelete()
	{
		System.out.println("번호 입력(0: 전체삭제, 1: 개별삭제); ");
		int deletenum = scan.nextInt();

		if(deletenum==0){
			for(int q=0; q<address.count; q++){
				address.collection[q] = null;
				alastName[q] = null;
				afirstName[q] = null;		
				Name[q] = null;
				aposition[q] = null;
				adepartment[q] = null;
				acompanyname[q] = null;
				aaddress[q] = null;
				atelephonenumber[q] = null;
				afaxnumber[q] = null;
				amobilenumber[q] = null;
				aemail[q] = null;
				ahomepage[q] = null;
			}
			n = 0;
			address.count=0;
		}

		else{
			deletenum =0;
			scan.nextLine();
			System.out.println("삭제하고 싶은 사람의 lastname firstname(사이의 공백 입력)을 입력하세요");
			String name = scan.nextLine();
			Scanner nameScan;
			String namestr[] = new String[2];

			int i =0;

			System.out.println("");
			System.out.println("------------------------------------------------------------------------------------------------------------");
			System.out.println("Number|Lastname|Firstname|Position|Department|Address|Telephonenumber|Faxnumber|Mobilenumber|E=mail|Homepage");
			System.out.print("\n");

			nameScan = new Scanner (name); //파일에 있는 한줄을 받아서 저장함
			nameScan.useDelimiter(" ");  //입력받은 내용을 " " 을 기준으로 나눔


			while (nameScan.hasNext()){
				/*파일로 입력받은 한 줄이 끝날 때 까지 각각을 str배열에 저장함*/
				namestr[0] = nameScan.next();
				namestr[1] = nameScan.next();
			}

			for(i=1; i<address.count+1; i++)
			{
				if(alastName[i-1].equals(namestr[0])){
					if(afirstName[i-1].equals(namestr[1])){
						System.out.print(i + "\t");
						System.out.println(address.collection[i-1]);
					}
				}
			}

			System.out.print("삭제하고 싶은 사람의 번호를 입력하세요: ");
			int inputDelete = scan.nextInt();
			for(int p=inputDelete-1; p<address.count-1; p++)
			{
				address.collection[p]=address.collection[p+1];
				alastName[p] = alastName[p+1];
				afirstName[p] = afirstName[p+1];
				Name[p] = Name[p+1];
				aposition[p] = aposition[p+1];
				adepartment[p] = adepartment[p+1];
				acompanyname[p] = acompanyname[p+1];
				aaddress[p] = aaddress[p+1];
				atelephonenumber[p] = atelephonenumber[p+1];
				afaxnumber[p] = afaxnumber[p+1];
				amobilenumber[p] = amobilenumber[p+1];
				
				address.collection[p+1]=null;
			}
			address.count--;
			n--;
		}

		scan.nextLine();			
		System.out.println("삭제 되었습니다.");

	}

	public void addrSave()
	{
		String file = "test.txt";
		String save = "";

		try{
			FileWriter fw = new FileWriter (file,false);
			BufferedWriter bw = new BufferedWriter (fw);
			PrintWriter outFile = new PrintWriter (bw);

			for(int i=0; i<address.count; i++)
			{
				save += address.collection[i];
				save += "\n";
			}

			bw.write(save);
			outFile.close();
		}
		catch(Exception n){}
	}

	public void pwSave(int pw)
	{
		String file = "pw.txt";
		String save = "";

		try{
			FileWriter fw = new FileWriter (file,false);
			BufferedWriter bw = new BufferedWriter (fw);
			PrintWriter outFile = new PrintWriter (bw);

			save += pw;
			save += "\n";

			bw.write(save);
			outFile.close();
		}
		catch(Exception n){}
	}

	public int pwLoad()
	{
		String url="";
		Scanner fileScan;
		int pw = 0;

		try
		{
			String file = "pw.txt";
			fileScan = new Scanner (new File(file));

			while (fileScan.hasNext()) 
			{ 
				url = fileScan.nextLine();
			}

			pw = Integer.parseInt(url);
		}
		catch(Exception n){}	

		return pw;
	}

	public void addrLoad()
	{
		String url; 
		Scanner fileScan, urlScan;
		String str[] = new String[11];

		try{
			String file = "test.txt";
			fileScan = new Scanner (new File(file));

			while (fileScan.hasNext()) 
			{ 
				url = fileScan.nextLine();  
				urlScan = new Scanner (url); 
				urlScan.useDelimiter("/"); 


				while (urlScan.hasNext()){
					str[0] = urlScan.next();
					str[1] = urlScan.next();
					str[2] = urlScan.next();
					str[3] = urlScan.next();
					str[4] = urlScan.next();
					str[5] = urlScan.next();
					str[6] = urlScan.next();
					str[7] = urlScan.next();
					str[8] = urlScan.next();
					str[9] = urlScan.next();
					str[10] = urlScan.next();

					address.addAddr(str[0],str[1],str[2],str[3],str[4],str[5],str[6],str[7],str[8],str[9],str[10]);

					Name[n] = str[0].concat(str[1]);
					alastName[n] = str[0];
					afirstName[n] = str[1];
					aposition[n] = str[2];
					adepartment[n] = str[3];
					acompanyname[n] = str[4];
					aaddress[n] = str[5];
					atelephonenumber[n] = str[6];
					afaxnumber[n] = str[7];
					amobilenumber[n] = str[8];
					aemail[n] = str[9];
					ahomepage[n] = str[10];

					n++;
				}

			}
		}
		catch(Exception n){}	
	}
}